package com.sx.gongzhong.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by XZY on 2017/12/28.
 * textView 左边
 */

public class DrawLeftText extends TextView {

    public DrawLeftText(Context context) {
        super(context);
    }

    public DrawLeftText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //得到Drawable集合  分别对应 左上右下
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            //获取左边图片
            Drawable drawableLeft= drawables[0];
            if (drawableLeft != null) {
                //获取控件大小
                int width=getWidth();
                //获取文字占用长宽
                int textWidth = (int) getPaint().measureText(getText().toString());
                //int textHeight = (int) getPaint().getTextSize();
                //获取图片实际长宽
                int drawableWidth  = drawableLeft.getIntrinsicWidth();
                int drawableHeight = drawableLeft.getIntrinsicHeight();
                //setBounds修改Drawable在View所占的位置和大小,对应参数同样的 左上右下()
                //向右偏移
                int left=(width-textWidth)/2-drawableWidth-5;
                drawableLeft.setBounds(left,0,drawableWidth+left,drawableHeight);
            }
//            Drawable drawableBottom= drawables[3];
//            if(drawableBottom!=null){
//                //获取控件大小
//                int width=getWidth();
//                int drawableHeight = drawableBottom.getIntrinsicHeight();
//                drawableBottom.setBounds(-width,0,width,drawableHeight);
//            }
        }
        super.onDraw(canvas);
    }
}
